package com.tmsps.scan.result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuScannerResult {

	public static List<Map<String, Object>> mcodeAndUrl = new ArrayList<>();
	
	public static Map<String, Object> getPageUrl(int pageNum,int pageSize) {
		int start = MenuScannerResult.getStart(pageNum, pageSize);
		List<Map<String, Object>> listGet = MenuScannerResult.mcodeAndUrl;
		List<Map<String, Object>> result = new ArrayList<>();
		int len = start+pageSize;
		long totalSize = listGet.size();
		for(int index=start;index<len;index++){
			if (index<totalSize)
				result.add(listGet.get(index));
		}
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("pageNumber", pageNum);
		resultMap.put("pageSize", pageSize);
		resultMap.put("totalPage", MenuScannerResult.getTotalPage(pageSize, (int)totalSize));
		resultMap.put("totalRow", totalSize);
		resultMap.put("list", result);
		return resultMap;
	}
	
	private static int getStart(int pageNum,int pageSize) {
		if (pageNum <=1) {
			return 0;
		}
		if (pageNum>1) {
			pageNum--;
		}
		return pageNum*pageSize;
	}
	
	private static int getTotalPage(int pageSize, int listSize) {
		return (listSize + pageSize -1) / pageSize;
	}
}
